package com.ccp.dev.qbdms.tag;

import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.service.DictionaryService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.Arrays;
import java.util.List;

/**
 * @author 米利昊
 * @description 问题责任单位标签
 */
public class DutyDeptTag extends SimpleTagSupport {

    /**
     * 下拉列表控件的名称
     */
    private String css;
    private String showIn;

    /**
     * 下拉列表控件的名称
     */
    private String id;

    /**
     * 下拉列表控件的名称
     */
    private String name;

    private String nowItemValue;
    private String verify;
    /**
     * 是否可选
     */
    private Boolean activity = true;

    /**
     * 提示信息
     */
    private String reqtext;

    /**
     * 验证
     */
    private Boolean search = false;

    /**
     * 是否只显示下级单位
     */
    private Boolean db = false;

    /**
     * 增加lay-filter
     */
    private String filter;

    public String getCss() {
        return css;
    }

    public void setCss(String css) {
        this.css = css;
    }

    public String getShowIn() {
        return showIn;
    }

    public void setShowIn(String showIn) {
        this.showIn = showIn;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNowItemValue() {
        return nowItemValue;
    }

    public void setNowItemValue(String nowItemValue) {
        this.nowItemValue = nowItemValue;
    }

    public String getVerify() {
        return verify;
    }

    public void setVerify(String verify) {
        this.verify = verify;
    }

    public Boolean getActivity() {
        return activity;
    }

    public void setActivity(Boolean activity) {
        this.activity = activity;
    }


    public String getReqtext() {
        return reqtext;
    }

    public void setReqtext(String reqtext) {
        this.reqtext = reqtext;
    }

    public Boolean getSearch() {
        return search;
    }

    public void setSearch(Boolean search) {
        this.search = search;
    }

    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public Boolean getDb() {
        return db;
    }

    public void setDb(Boolean db) {
        this.db = db;
    }

    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        try {
            int numberLevel = BaseUtil.getCurrentSystemNumberLevel();
            StringBuffer stringBuffer = new StringBuffer("<select ");
            if (null == showIn) {
                showIn = "0,1";
            }
            if (id != null) {
                stringBuffer.append("id=\"" + id + "\" ");
            }
            if (name != null) {
                stringBuffer.append("name=\"" + name + "\" ");
            }
            if (css != null) {
                stringBuffer.append("class=\"" + css + "\" ");
            }
            if (verify != null) {
                stringBuffer.append(" lay-verify=\"" + verify + "\" ");
            }
            if(!activity){
                stringBuffer.append(" disabled ");
            }
            if (reqtext != null) {
                stringBuffer.append(" lay-reqtext=\"" + reqtext + "\" ");
            }
            if (search) {
                stringBuffer.append(" lay-search ");
            }
            if (filter != null) {
                stringBuffer.append(" lay-filter= \"" + filter + "\" ");
            }
            stringBuffer.append(">");
            stringBuffer.append("<option value=" + "");
            stringBuffer.append(">请选择</option>");
            List<String> showInList = Arrays.asList(showIn.split(","));
            if (showInList.contains(String.valueOf(numberLevel)) && "2".equals(String.valueOf(numberLevel))) {
                DictionaryService dictionaryService = AppUtil.getBean(DictionaryService.class);
                List<Dictionary> list = dictionaryService.getByNodeKey("sjdwzd");
                for (Dictionary dic : list) {
                    stringBuffer.append("<option value=" + dic.getItemValue());
                    if (dic.getItemValue().equals(nowItemValue)) {
                        stringBuffer.append(" selected ");
                    }
                    stringBuffer.append(">" + dic.getItemName() + "</option>");
                }
            } else if (showInList.contains(String.valueOf(numberLevel))) {
                stringBuffer.append("<option></option>");
                ExtendSysOrgService extendSysOrgService = AppUtil.getBean(ExtendSysOrgService.class);
                // 默认查询所有下级单位
                List<Dictionary> originOrgToDicList = extendSysOrgService.getAllSubUnits();
                if (db) {
                    // -- 只查询下级单位 并非所有
                    originOrgToDicList = extendSysOrgService.getSubordinateUnits();
                }
                originOrgToDicList.forEach(dictionary -> {
                    stringBuffer.append("<option  ").append("orgcode=\"").append(dictionary.getItemValue()+"\"").append(" value=\"").append(dictionary.getItemName()).append("\">").append(dictionary.getItemName()).append("</option>");
                });
            }
            stringBuffer.append("</select>");
            out.print(stringBuffer);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
